package com.xiaoxi.exam.controller;

import cn.xinzhili.xutils.core.http.Response;
import com.xiaoxi.exam.bean.enums.MessageType;
import com.xiaoxi.exam.model.Message;
import com.xiaoxi.exam.service.MessageService;
import com.xiaoxi.exam.utils.MessageFactory;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/message")
@Api(description = "消息相关接口")
public class MessageController {

  @Autowired
  private MessageService messageService;

  @ApiOperation(value = "获取消息列表")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "openId", value = "openId", dataType = "String", paramType = "query"),
      @ApiImplicitParam(name = "type", value = "消息类型", dataType = "String", paramType = "query"),
  })
  @PostMapping
  public Response getMessage(@RequestParam("openId") String openId,
      @RequestParam(value = "type", defaultValue = "SYSTEM")
          MessageType type) {
    List<Message> messages = messageService.getMessageByOpenId(openId, type);
    return Response.instanceSuccess(MessageFactory.toMessageInfoListResponse(messages));
  }

  @ApiOperation(value = "获取消息详情")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "id", value = "id", dataType = "String", paramType = "query")
  })
  @PostMapping("/get")
  public Response getMessageById(@RequestParam("id") String id) {
    Message messageById = messageService.getMessageById(id);
    return Response.instanceSuccess(MessageFactory.getMessageInfo(messageById));
  }

}
